package com.mg.framework.kernel;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;

public abstract class Actor extends BaseActor {

	protected int width;
	protected int height;
	protected Bitmap bitmap;
	protected Canvas canvas;
	protected Screen screen;

	public Actor(Screen screen, Bitmap bitmap) {
		super();
		this.bitmap = bitmap;
		initActor(screen);
	}

	public Actor(Screen screen, int width, int height) {
		super();
		this.bitmap = Bitmap.createBitmap(width, height,
				Bitmap.Config.ARGB_8888);
		initActor(screen);
	}

	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height;
	}

	public Screen getScreen() {
		return this.screen;
	}

	private void initActor(Screen screen) {
		this.screen = screen;
		this.width = this.bitmap.getWidth();
		this.height = this.bitmap.getHeight();
		this.canvas = new Canvas(this.bitmap);
	}

	protected void drawSelf(Canvas canvas, Paint newPaint) {
		super.drawSelf();
		if (newPaint == null) {
			canvas.drawBitmap(this.bitmap, this.matrix, this.paint);
			return;
		}
		canvas.drawBitmap(this.bitmap, this.matrix, newPaint);
	}

	protected void flipHorizontally() {
		this.bitmap = this.getNewFlipHorizontallyBitmap();
		this.canvas = new Canvas(this.bitmap);

	}

	protected void flipVertically() {
		this.bitmap = this.getNewFlipVerticallyBitmap();
		this.canvas = new Canvas(this.bitmap);
	}

	private Bitmap getNewFlipHorizontallyBitmap() {
		Bitmap newBitmap = Bitmap.createBitmap(this.bitmap, 0, 0, this.width,
				this.height, flipHorizontally(this.width, this.height), true);
		return newBitmap;
	}

	private Bitmap getNewFlipVerticallyBitmap() {
		Bitmap newBitmap = Bitmap.createBitmap(this.bitmap, 0, 0, this.width,
				this.height, flipVertically(this.width, this.height), true);
		return newBitmap;
	}

	protected void setWidthAndHeight(int width, int height) {
		this.width = width;
		this.height = height;
		this.bitmap = this.getNewWHBitmap();
		this.canvas = new Canvas(this.bitmap);
	}

	private Bitmap getNewWHBitmap() {
		Bitmap newBitmap = Bitmap.createScaledBitmap(this.bitmap, this.width,
				this.height, true);
		return newBitmap;
	}

	protected boolean defaultIsTouched(MotionEvent event) {
		if (event.getX() > this.getX()
				&& event.getX() < this.getX() + this.width
				&& event.getY() > this.getY()
				&& event.getY() < this.getY() + this.height) {
			return true;
		}
		return false;
	}
}